﻿Imports StartFrame.Web.BR.Helpers
Imports StartFrame.BR.Web.Helpers
Imports StartFrame.BR.Utilitarios

Namespace GLP.Abms

    Public Class Impo
        Inherits Abm

        ''' <summary>
        ''' Esta clase se encarga de la administración de los reportes o consultas contempladas por la aplicación.
        ''' </summary>
        ''' Constructor
        Sub New()
            MyBase.New(Operador, Password, "XRImpo", "App_articulo")
            MyBase.lnk_nombreOperador = Common.Env.Operador
            'Graba tracking
            Me.lnk_Tracking = True
        End Sub

        'Proceso de importación de un archivo CSV en una tabla de SQL
        Public Function Importar(FileContent As String, Tabla As String) As ResultSet
            Dim ds As New DataSet
            Dim dt As DataTable = DA.Sql.GetSchema(Common.Env.ConnectionString, Tabla)
            Dim sr As New IO.StringReader(FileContent)
            Dim strLine As String = sr.ReadLine()
            While Not strLine Is Nothing
                Dim strData() As String = strLine.Split(";")
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 1 To dt.Columns.Count - 1
                    If i = 1 Then
                        dr(i - 1) = New Talonarios().getNum(IIf(Tabla = "app_articulo", "ART", "DESREC"))
                    End If
                    dr(i) = strData(i - 1)
                    If String.IsNullOrWhiteSpace(strData(i - 1)) AndAlso dt.Columns(i).AllowDBNull Then
                        dr(i) = DBNull.Value
                    End If
                Next
                dt.Rows.Add(dr)
                strLine = sr.ReadLine()
            End While

            ds.Tables.Add(dt)
            If Tabla.ToLower().Equals("app_articulo") Then
                Dim art As New BR.Web.Mantenimiento.Articulos.Art
                art.ActualizarDatos(ds)
            ElseIf Tabla.ToLower().Equals("app_desrec") Then
                Dim desrec As New BR.Web.Mantenimiento.Precios.DesRec
                desrec.ActualizarDatos(ds)
            End If
            If (ds.HasErrors) Then
                Return New ResultSet() With {.success = False, .message = PrintAllErrs(ds)}
            End If
            Return New ResultSet() With {.success = True}
        End Function

        Private Function PrintAllErrs(ByVal dataSet As DataSet) As String
            Dim rowsInError() As DataRow
            Dim table As DataTable
            Dim i As Integer
            Dim column As DataColumn
            Dim strResult As New Text.StringBuilder
            For Each table In dataSet.Tables
                ' Test if the table has errors. If not, skip it. 
                If table.HasErrors Then
                    ' Get an array of all rows with errors.
                    rowsInError = table.GetErrors()
                    ' Print the error of each column in each row. 
                    For i = 0 To rowsInError.GetUpperBound(0)
                        For Each column In table.Columns
                            If Not String.IsNullOrEmpty(rowsInError(i).GetColumnError(column)) Then
                                strResult.AppendLine(String.Format("La fila {0} contiene el siguiente error: {1}", i + 1, rowsInError(i).GetColumnError(column)))
                            End If
                        Next
                        ' Clear the row errors
                        rowsInError(i).ClearErrors()
                    Next i
                End If
            Next
            Return strResult.ToString()
        End Function
    End Class

End Namespace
